package com.smart.buss.ctrl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.smart.buss.service.BussDakaLogService;
import com.smart.buss.service.BussDakaService;
import com.smart.buss.service.BussSetService;
import com.smart.buss.service.BussUserService;
import com.smart.buss.vo.BussDaka;
import com.smart.buss.vo.BussDakaLog;
import com.smart.buss.vo.BussSet;
import com.smart.buss.vo.BussUser;
import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.common.util.CheckClientUtil;
import com.smart.primary.common.util.DateUtil;
import com.smart.primary.util.SessionUtil;

@Controller
@RequestMapping("/buss/user")
public class BussLocaController extends BaseCtrl {
	private final static Logger logger= Logger.getLogger(BussLocaController.class);
	@Autowired
	private BussUserService bussUserService;
	/**
	 * 获取打卡信息
	 * http://ts.timxu.me/task/buss/user/loc?code=121212
	 * http://ts.timxu.me/task/buss/user/loc?t=h5&code=121212
	 *
	 */
	@RequestMapping("/loc")
	public void loc() throws Exception{
		String html = "";
		String u = request.getParameter("code");
		String t = request.getParameter("t");
		String channel = u;
		
		if("ios".equals(t)) {
			html = "/task/views/app/start-ios.html";
			SessionUtil.addCookie(response, "channel_code", u,false);
		}else if("ad".equals(t)) {
			if(CheckClientUtil.hasIphone(request)) {
				html = "/task/views/app/start-ios.html";
				SessionUtil.addCookie(response, "channel_code", u,false);
			}else {
				html = "/task/views/app/start.html";
				if(StringUtils.isNotEmpty(u)) {
					html += "?code="+u;
					String destPath = "/var/www/webapps/apk/u/";
					PackerNg.apkOne2("/var/www/webapps/apk/app-free-release.apk", u,destPath);
				}
			}
		}else {
			BussUser user = new BussUser();
			user.setCode(u);
			user = bussUserService.queryObject(user);
			if(user != null && StringUtils.isNotEmpty(user.getChannel())) {
				channel = user.getChannel();
			}
			html = "/task/views/app/index.html";
			SessionUtil.addCookie(response, "fcode", u,false);
			SessionUtil.addCookie(response, "channel_code", u,false);
			SessionUtil.addCookie(response, "channel", channel,false);
		}
		
		response.sendRedirect(html);
	}
 
}
